package org.bits.ui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import org.bits.common.IconConstants;
import org.bits.common.ui.JGoodiesMenuBar;
import org.bits.data.Finding;
import org.bits.data.Issue;
import org.bits.io.UserOptionsSerializer;
import org.bits.model.DataAccessModel;
import org.jutils.ui.ExitListener;
import org.jutils.ui.StatusBarPanel;
import org.jutils.ui.event.ItemActionEvent;
import org.jutils.ui.event.ItemActionListener;

/*******************************************************************************
 *
 ******************************************************************************/
public class BitsFrameView
{
    /**  */
    private StatusBarPanel statusBar;
    /**  */
    private JFrame bitsFrame;
    /**  */
    private ResultsView resultsView;

    /**  */
    private DataAccessModel dataModel;
    /**  */
    private UserOptionsSerializer optionsSerializer;

    /***************************************************************************
     * 
     **************************************************************************/
    public BitsFrameView( DataAccessModel dataModel,
        UserOptionsSerializer optionsSerializer )
    {
        this.dataModel = dataModel;
        this.optionsSerializer = optionsSerializer;

        statusBar = new StatusBarPanel();
        bitsFrame = new JFrame( "BITS" );
        resultsView = new ResultsView();

        bitsFrame.setJMenuBar( createMenuBar( bitsFrame ) );
        bitsFrame.setContentPane( createContentPane() );
        bitsFrame.setIconImages( IconConstants.getBitsImages() );
        bitsFrame.setExtendedState( JFrame.MAXIMIZED_BOTH );
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private JMenuBar createMenuBar( JFrame frame )
    {
        JGoodiesMenuBar menuBar = new JGoodiesMenuBar();
        JMenu menu;
        JMenuItem menuItem;

        menu = new JMenu( "File" );
        menu.setMnemonic( KeyEvent.VK_F );
        menuBar.add( menu );

        menuItem = new JMenuItem( "New Finding",
            IconConstants.loader.getIcon( IconConstants.NEW_FILE_16 ) );
        menuItem.setMnemonic( KeyEvent.VK_F );
        menuItem.addActionListener( new NewFindingButtonListener() );
        menu.add( menuItem );

        menuItem = new JMenuItem( "New Issue",
            IconConstants.loader.getIcon( IconConstants.NEW_BOOKMARK_16 ) );
        menuItem.setMnemonic( KeyEvent.VK_I );
        menuItem.addActionListener( new NewIssueButtonListener() );
        menu.add( menuItem );

        menuItem = new JMenuItem( "New Filter",
            IconConstants.loader.getIcon( IconConstants.NEW_FILTER_16 ) );
        menuItem.setMnemonic( KeyEvent.VK_L );
        menuItem.addActionListener( new NewFilterButtonListener() );
        menu.add( menuItem );

        menu.addSeparator();

        menuItem = new JMenuItem( "Exit",
            IconConstants.loader.getIcon( IconConstants.CLOSE_16 ) );
        menuItem.setMnemonic( KeyEvent.VK_X );
        menuItem.addActionListener( new ExitListener( frame ) );
        menu.add( menuItem );

        menu = new JMenu( "View" );
        menu.setMnemonic( KeyEvent.VK_V );
        menuBar.add( menu );

        menuItem = new JMenuItem( "Products" );
        menuItem.setMnemonic( KeyEvent.VK_P );
        menuItem.addActionListener( new ProductsButtonListener() );
        menu.add( menuItem );

        menuItem = new JMenuItem( "Users" );
        menuItem.setMnemonic( KeyEvent.VK_U );
        menuItem.addActionListener( new UsersButtonListener() );
        menu.add( menuItem );

        return menuBar;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private Container createContentPane()
    {
        JPanel contentPanel = new JPanel( new BorderLayout() );
        FilterView filterView = new FilterView( dataModel, optionsSerializer );
        JSplitPane splitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT );

        filterView.addFindingSelectedListener( new FindingSelectedListener() );
        filterView.addEditFindingListener( new EditFindingListener() );
        filterView.addClearListener( new ClearItemListener() );

        splitPane.setBorder( null );
        splitPane.setTopComponent( filterView.getView() );
        splitPane.setBottomComponent( resultsView.getView() );
        splitPane.setDividerLocation( 200 );
        splitPane.setOneTouchExpandable( true );

        contentPanel.add( splitPane, BorderLayout.CENTER );
        contentPanel.add( statusBar.getView(), BorderLayout.SOUTH );

        return contentPanel;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public JFrame getView()
    {
        return bitsFrame;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class ProductsButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent arg0 )
        {
            ProductsView productsView = new ProductsView();
            OkCancelDialog dialog = new OkCancelDialog( getView(), "Products",
                productsView.getView() );

            productsView.setDataModel( dataModel );

            dialog.setSize( 300, 300 );

            if( dialog.showDialogView() )
            {
                // TODO Do something.
            }
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class UsersButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent arg0 )
        {
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class NewIssueButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent arg0 )
        {
            JPanel scrollPanel;
            GridBagConstraints constraints;
            IssueView issuePanel;
            JScrollPane issueScrollPane;
            OkCancelDialog okCancelDialog;

            scrollPanel = new JPanel( new GridBagLayout() );
            issuePanel = new IssueView( true );
            issueScrollPane = new JScrollPane( scrollPanel );
            okCancelDialog = new OkCancelDialog( getView(), "New Issue",
                issueScrollPane );

            issuePanel.setIssue( new Issue() );

            constraints = new GridBagConstraints( 0, 0, 1, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
                    4, 4, 4, 4 ), 0, 0 );
            scrollPanel.add( issuePanel.getView(), constraints );

            issueScrollPane.setBorder( null );

            if( okCancelDialog.showDialogView() )
            {
                dataModel.addIssue( issuePanel.getIssue() );
                ; // TODO do something with the issue.
            }
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class NewFindingButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent arg0 )
        {
            ScrollableOkCancelDialog dialog;
            FindingView findingPanel;

            findingPanel = new FindingView( dataModel, true );
            dialog = new ScrollableOkCancelDialog( getView(), "New Finding",
                findingPanel.getView() );

            findingPanel.setFinding( new Finding() );

            dialog.setSize( 500, 500 );

            if( dialog.showDialogView() )
            {
                findingPanel.apply();
                dataModel.addFinding( findingPanel.getFinding() );
                ; // TODO Do something with the finding.
            }
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class NewFilterButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent arg0 )
        {
            ;
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class EditFindingListener implements ItemActionListener<Finding>
    {
        @Override
        public void actionPerformed( ItemActionEvent<Finding> event )
        {
            ScrollableOkCancelDialog dialog;
            FindingView findingPanel;

            findingPanel = new FindingView( dataModel, false );
            dialog = new ScrollableOkCancelDialog( getView(), "New Finding",
                findingPanel.getView() );

            findingPanel.setFinding( event.getItem() );

            dialog.setSize( 500, 500 );

            if( dialog.showDialogView() )
            {
            }
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class FindingSelectedListener implements
        ItemActionListener<Finding>
    {
        @Override
        public void actionPerformed( ItemActionEvent<Finding> event )
        {
            resultsView.setFinding( event.getItem() );
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class ClearItemListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            resultsView.clear();
        }
    }
}
